﻿using System;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Com.Core.Helps
{
    public class EncryptHelper
    {
        // ReSharper disable once InconsistentNaming
        /// <summary>
        /// AES对称加密算法
        /// </summary>
        public static class AES
        {
            //默认密钥向量 
            private static readonly byte[] AesKeys = { 0xB4, 0xD3, 0x7E, 0x06, 0xE8, 0xF0, 0xCD, 0xCA, 0x8C, 0xEA, 0xEE, 0xFA, 0x9C, 0x12, 0xDA, 0xA3 };
            
            private static readonly byte[] _AesKeys = { 0x3F, 0xBB, 0x4E, 0xE9, 0xBC, 0xCF, 0x45, 0x48, 0xDA, 0xB4, 0x22, 0xAE, 0x36, 0xE1, 0xB3, 0x3E, 0x9E, 0x0D, 0xDF, 0x41, 0xE9, 0xA9, 0x9D, 0x7E, 0x45, 0x91, 0x5D, 0xEA, 0xF6, 0x22, 0xE9, 0x7D, 0x38, 0x8C, 0xC9, 0xA3, 0xAC, 0xAE, 0x91, 0x22, 0xE9, 0x7D, 0x38, 0x8C, 0xC9, 0xA3, 0xAC, 0xAE, 0xB4, 0x22, 0x47, 0x36, 0xE1, 0xFF, 0xBB, 0x4E, 0xE9, 0xBC, 0xCF, 0x04, 0x48, 0xDA, 0xB4, 0x22 };
            private static string CreateAesKey()
            {
                string str = Convert.ToBase64String(_AesKeys).Substring(0, 32);
                return str;
            }
            private static readonly string AesKey = CreateAesKey();

            /// <summary>  
            /// AES加密算法  
            /// </summary>  
            /// <param name="encryptString">明文字符串</param>
            /// <param name="encryptKey">密钥</param>
            /// <returns>返回加密后的密文字节数组</returns>
            // ReSharper disable once MemberHidesStaticFromOuterClass
            public static string Encrypt(string encryptString, string encryptKey = "")
            {
                encryptKey = encryptKey == "" ? AesKey : encryptKey;
                //int keyLength = encryptKey.Length;
                //System.Diagnostics.Debug.Assert(keyLength % 16 == 0, "密匙错误");
                //if (keyLength % 16 != 0) return encryptString;
                //return _AESKeys.Length.ToString() + "<br>" + Convert.ToBase64String(_AESKeys).Length.ToString() + "<br>" + encryptKey.Length;
                RijndaelManaged rijndaelProvider = new RijndaelManaged
                {
                    Key = Encoding.UTF8.GetBytes(encryptKey),
                    IV = AesKeys
                };
                ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();

                byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
                byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);

                return Convert.ToBase64String(encryptedData);
            }

            public static string AdvancEncrypt(string encryptString, string encryptKey = "")
            {
                var str = RandomHelper.CreateRandom(5, 20, RandomCharType.Charaters) + "@_@" + encryptString + "@_@" + RandomHelper.CreateRandom(5, 20, RandomCharType.Charaters);
                return Encrypt(str, encryptKey);
            }

            /// <summary>  
            /// AES解密  
            /// </summary>  
            /// <param name="decryptString">密文字节数组</param>  
            /// <param name="decryptKey">密钥</param>  
            /// <returns>返回解密后的字符串</returns>  
            // ReSharper disable once MemberHidesStaticFromOuterClass
            public static string Decrypt(string decryptString, string decryptKey = "")
            {
                decryptKey = decryptKey == "" ? AesKey : decryptKey;
                int keyLength = decryptKey.Length;
                Debug.Assert(keyLength % 16 == 0, "密匙错误");
                if (keyLength % 16 != 0) return decryptString;
                try
                {
                    RijndaelManaged rijndaelProvider = new RijndaelManaged
                    {
                        Key = Encoding.UTF8.GetBytes(decryptKey),
                        IV = AesKeys
                    };
                    ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();

                    byte[] inputData = Convert.FromBase64String(decryptString);
                    byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);

                    return Encoding.UTF8.GetString(decryptedData);
                }
                catch
                {
                    return decryptString;
                }
            }

            public static string AdvanceDectypt(string decryptString, string decryptKey = "")
            {
                var str = Decrypt(decryptString, decryptKey);
                try
                {
                    var temp = Regex.Split(str, "@_@", RegexOptions.IgnoreCase);
                    return temp[1];
                }
                catch
                {
                    return "";
                }
            }
        }


        // ReSharper disable once InconsistentNaming
        /// <summary>
        /// RSA对称加密算法
        /// </summary>
        public static class RSA
        {
            
        }

        public static string Sha1(string str)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] inbyteBytes = Encoding.UTF8.GetBytes(str);
            byte[] outbyBytes = sha1.ComputeHash(inbyteBytes);
            string outstr = BitConverter.ToString(outbyBytes);
            //str_sha1_out = str_sha1_out.Replace("-", "");
            return outstr.Replace("-", "");
        }
    }
}
